package com.yxt.ui;

import com.yxt.common.ui.ActivityStackControlUtil;
import com.yxt.common.ui.ImageUtil;
import com.yxt.common.ui.MulitPointTouchListener;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;  
import android.widget.ImageView;

public class WaterStationLegendActivity extends Activity {
	private static final int MENU_LINE = Menu.FIRST+1;
	private static final int MENU_LIST = Menu.FIRST+2;
	private ImageView imageLegend;
	private String code;
	private String type;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
		setContentView(R.layout.water_legend);   //软件activity的布局	
		ActivityStackControlUtil.add(this);
		initComponent();
	}
	
	private void initComponent(){
		Button btnBackHome = (Button)findViewById(R.id.btnBackHome);		
		btnBackHome.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MainActivity.class);
				startActivity(intent);
			}
		});
		Intent intent = getIntent();
		code = intent.getStringExtra("code");
		type = intent.getStringExtra("type");
		imageLegend = (ImageView)findViewById(R.id.imageLegend);
		imageLegend.setOnTouchListener(new MulitPointTouchListener());
		zoom();
	}

	private void zoom() {
		int screenWidth, screenHeight;
		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		screenWidth = display.getWidth();
		screenHeight = display.getHeight();
		imageLegend.setImageDrawable(ImageUtil.zoomPicture(
				getApplicationContext(), R.drawable.water_legend, screenWidth, screenHeight, "10", "10", "2"));
	}

	/**
	 * 创建底部菜单
	 * */
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_LINE, 1, R.string.menu_line);
		menu.add(0, MENU_LIST, 2, R.string.menu_list);
		return super.onCreateOptionsMenu(menu);
	}
	
	/**
	 * 点击底部菜单项时的处理
	 */
	public boolean onOptionsItemSelected(MenuItem item) {	
		Intent intent = new Intent();	
		intent.putExtra("code", code);
		intent.putExtra("type", type);
		switch (item.getItemId()) {
			case MENU_LINE:
				intent.setClass(getApplicationContext(), WaterStationChartActivity.class);
				startActivity(intent);
				return true;
			case MENU_LIST:
				intent.setClass(getApplicationContext(), WaterStationDataActivity.class);
				startActivity(intent);
				return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void onConfigurationChanged(Configuration newConfig) {
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// 当前为横屏， 在此处添加额外的处理代码
			zoom();
		} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			// 当前为竖屏， 在此处添加额外的处理代码
			zoom();
		}
		super.onConfigurationChanged(newConfig);
	}

	protected void onDestroy() {
		super.onDestroy();
        ActivityStackControlUtil.remove(this);
	}
}